<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/6/26
 * Time: 0:49
 */

namespace app\common\lib;

use app\common\lib\ali\Sms;
use think\Cache;
use think\Log;

class Alidayu
{

    const  LOG_TPL = "AliDaYu:";

    /**
     * 静态变量保存全局实例
     * @var null
     */
    private static $_instance = null;

    /**
     * 私有的构造方法
     * Alidayu constructor.
     */
    private function __construct(){
    }

    /**
     * 静态方法  单例模式统一入口
     * @return Alidayu|null
     */
    public static function getInstance(){
        if (is_null(self::$_instance)){
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    /**
     * 设置短信验证
     * @param int $phoneNum
     * @return bool
     */
    public function setSmsIdentify($phoneNum = 0){
        //tood
        $code = rand(1000, 9999);

        try {
            $response = Sms::sendSms($phoneNum,$code);
        } catch (\Exception $e) {
            //记录日志
            Log::write(self::LOG_TPL."set----".$e->getMessage());
            return false;
        }

        if($response->Code == "OK"){
            //设置验证码时效时间
            Cache::set($phoneNum, $code, config('aliyun.identify_time'));
            return true;
        }else{
            Log::write(self::LOG_TPL."set:errror --->" . json_encode($response));
            return false;
        }
    }

    /**
     * 根据手机号码查询验证码是否正常
     * @param int $phone
     * @return bool|mixed
     */
    public function checkSmsIdentify( $phone = 0){
        if(!$phone){
            return false;
        }
        return Cache::get($phone);
    }

}